//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.server;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.MessagingException;

import tv.dyndns.kishibe.client.Service;
import tv.dyndns.kishibe.client.ServiceException;
import tv.dyndns.kishibe.client.constant.Constant;
import tv.dyndns.kishibe.client.game.GameMode;
import tv.dyndns.kishibe.client.game.ProblemGenre;
import tv.dyndns.kishibe.client.game.ProblemType;
import tv.dyndns.kishibe.client.game.RandomFlag;
import tv.dyndns.kishibe.client.packet.NewAndOldProblems;
import tv.dyndns.kishibe.client.packet.PacketBbsResponse;
import tv.dyndns.kishibe.client.packet.PacketBbsThread;
import tv.dyndns.kishibe.client.packet.PacketChatData;
import tv.dyndns.kishibe.client.packet.PacketChatDataList;
import tv.dyndns.kishibe.client.packet.PacketGameStatus;
import tv.dyndns.kishibe.client.packet.PacketImageLink;
import tv.dyndns.kishibe.client.packet.PacketLinkData;
import tv.dyndns.kishibe.client.packet.PacketLogin;
import tv.dyndns.kishibe.client.packet.PacketMatchingData;
import tv.dyndns.kishibe.client.packet.PacketPlayerSummary;
import tv.dyndns.kishibe.client.packet.PacketProblem;
import tv.dyndns.kishibe.client.packet.PacketProblemCreationLog;
import tv.dyndns.kishibe.client.packet.PacketRankingData;
import tv.dyndns.kishibe.client.packet.PacketRatingDistribution;
import tv.dyndns.kishibe.client.packet.PacketReadyForGame;
import tv.dyndns.kishibe.client.packet.PacketRegistrationData;
import tv.dyndns.kishibe.client.packet.PacketResult;
import tv.dyndns.kishibe.client.packet.PacketRoomKey;
import tv.dyndns.kishibe.client.packet.PacketServerStatus;
import tv.dyndns.kishibe.client.packet.PacketTheme;
import tv.dyndns.kishibe.client.packet.PacketThemeModeEditor;
import tv.dyndns.kishibe.client.packet.PacketUserData;
import tv.dyndns.kishibe.client.packet.PacketWrongAnswer;
import tv.dyndns.kishibe.server.database.Database;
import tv.dyndns.kishibe.server.database.DatabaseException;
import tv.dyndns.kishibe.server.handwriting.Recognizable;
import tv.dyndns.kishibe.server.sns.SnsClient;
import tv.dyndns.kishibe.server.sns.SnsClients;
import tv.dyndns.kishibe.server.util.IntArray;
import tv.dyndns.kishibe.server.websocket.WebSocketServer;

import com.google.common.base.Objects;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

@SuppressWarnings("serial")
@Singleton
public class ServiceImpl extends RemoteServiceServlet implements Service {
	private static final Logger logger = Logger.getLogger(ServiceImpl.class.toString());
	private static final File PROBLEM_CREATION_LOG_FILE = new File("/tmp/qmaclone/problem.log");
	private final Random random = new Random();
	private final ChatManager chatManager;
	private final NormalModeProblemManager normalModeProblemManager;
	private final ThemeModeProblemManager themeModeProblemManager;;
	private final GameManager gameManager;
	private final ServerStatusManager serverStatusManager;
	private final BadUserManager badUserManager;
	private final PlayerHistoryManager playerHistoryManager;
	private final VoteManager voteManager;
	private final Recognizable recognizer;
	private final ThemeModeEditorManager themeModeEditorManager;
	private final WebSocketServer webSocketServer;
	private final ImageManager imageManager;
	private final Database database;
	private final PrefectureRanking prefectureRanking;
	private final RatingDistribution ratingDistribution;
	private final SnsClient snsClient;
	private final GameLogger gameLogger;
	private final ThreadPool threadPool;

	/**
	 * Only for testing.
	 */
	public ServiceImpl() {
		Injector injector = Guice.createInjector(new QMACloneModule());
		this.chatManager = injector.getInstance(ChatManager.class);
		this.normalModeProblemManager = injector.getInstance(NormalModeProblemManager.class);
		this.themeModeProblemManager = injector.getInstance(ThemeModeProblemManager.class);
		this.gameManager = injector.getInstance(GameManager.class);
		this.serverStatusManager = injector.getInstance(ServerStatusManager.class);
		this.badUserManager = injector.getInstance(BadUserManager.class);
		this.playerHistoryManager = injector.getInstance(PlayerHistoryManager.class);
		this.voteManager = injector.getInstance(VoteManager.class);
		this.recognizer = injector.getInstance(Recognizable.class);
		this.themeModeEditorManager = injector.getInstance(ThemeModeEditorManager.class);
		this.webSocketServer = injector.getInstance(WebSocketServer.class);
		this.imageManager = injector.getInstance(ImageManager.class);
		this.database = injector.getInstance(Database.class);
		this.prefectureRanking = injector.getInstance(PrefectureRanking.class);
		this.ratingDistribution = injector.getInstance(RatingDistribution.class);
		this.snsClient = injector.getInstance(SnsClients.class);
		this.gameLogger = injector.getInstance(GameLogger.class);
		this.threadPool = injector.getInstance(ThreadPool.class);
	}

	@Inject
	public ServiceImpl(ChatManager chatManager, NormalModeProblemManager normalModeProblemManager,
			ThemeModeProblemManager themeModeProblemManager, GameManager gameManager,
			ServerStatusManager serverStatusManager, BadUserManager badUserManager,
			PlayerHistoryManager playerHistoryManager, VoteManager voteManager,
			Recognizable recognizer, ThemeModeEditorManager themeModeEditorManager,
			WebSocketServer webSocketServer, ImageManager imageManager, Database databse,
			PrefectureRanking prefectureRanking, RatingDistribution ratingDistribution,
			@Named("SnsClients") SnsClient snsClient, GameLogger gameLogger, ThreadPool threadPool) {
		this.chatManager = chatManager;
		this.normalModeProblemManager = normalModeProblemManager;
		this.themeModeProblemManager = themeModeProblemManager;
		this.gameManager = gameManager;
		this.serverStatusManager = serverStatusManager;
		this.badUserManager = badUserManager;
		this.playerHistoryManager = playerHistoryManager;
		this.voteManager = voteManager;
		this.recognizer = recognizer;
		this.themeModeEditorManager = themeModeEditorManager;
		this.webSocketServer = webSocketServer;
		this.imageManager = imageManager;
		this.database = databse;
		this.prefectureRanking = prefectureRanking;
		this.ratingDistribution = ratingDistribution;
		this.snsClient = snsClient;
		this.gameLogger = gameLogger;
		this.threadPool = threadPool;

		// テーマモードのTwitter通知タイミング調整
		threadPool.scheduleWithFixedDelay(commandUpdateThemeModeNotificationCounter, 1, 1,
				TimeUnit.SECONDS);
		try {
			webSocketServer.start();
		} catch (Exception e) {
			logger.log(Level.WARNING, "WebSocketの開始に失敗しました", e);
		}
	}

	@Override
	public String processCall(String payload) {
		// すべての例外を補足してログに出力する
		try {
			return super.processCall(payload);
		} catch (Throwable e) {
			String message = "ハンドルされていないエラーが発生しました: "
					+ Objects.toStringHelper(this).add("remoteAddress", getRemoteAddress())
							.add("rpcRequest", RPC.decodeRequest(payload, null, this))
							.add("payload", payload).toString();
			logger.log(Level.WARNING, message, e);
			try {
				return RPC.encodeResponseForFailure(null,
						new ServiceException(Throwables.getStackTraceAsString(e)));
			} catch (SerializationException e1) {
				logger.log(Level.SEVERE, "エラー情報の返信に失敗しました", e1);
			}
		}
		return "";
	}

	@Override
	public void destroy() {
		try {
			webSocketServer.stop();
		} catch (Exception e) {
			logger.log(Level.WARNING, "WebSocketの停止に失敗しました", e);
		}
		try {
			webSocketServer.join();
		} catch (InterruptedException e) {
			logger.log(Level.WARNING, "WebSocketの停止待機に失敗しました", e);
		}
		threadPool.shutdown();
		super.destroy();
	}

	@Override
	public PacketServerStatus getServerStatus() {
		return serverStatusManager.getServerStatus();
	}

	@Override
	public PacketLogin login(int userCode) {
		serverStatusManager.login();
		serverStatusManager.keepAlive(userCode);
		PacketLogin login = new PacketLogin();
		login.removeAddress = getRemoteAddress();
		return login;
	}

	@Override
	public void keepAlive(int userCode) {
		serverStatusManager.keepAlive(userCode);
	}

	// プレイヤー情報を登録する
	private Object lockObjectRegister = new Object();

	@Override
	public PacketRegistrationData register(PacketPlayerSummary playerSummary,
			Set<ProblemGenre> genres, Set<ProblemType> types, String greeting, GameMode gameMode,
			String roomName, String theme, String imageFileName, int classLevel,
			int difficultSelect, int rating, int userCode, int volatility, int playCount,
			NewAndOldProblems newAndOldProblems, boolean publicEvent) {
		synchronized (lockObjectRegister) {
			try {
				gameLogger.write(Objects.toStringHelper(this).add("method", "register")
						.add("userCode", userCode).toString());

				playerHistoryManager.push(playerSummary);

				PlayerStatus status;
				Game session = gameManager.getOrCreateMatchingSession(gameMode, roomName,
						classLevel, theme, genres, types, publicEvent, serverStatusManager,
						userCode, getRemoteAddress(), badUserManager);
				status = session.addPlayer(playerSummary, genres, types, greeting, imageFileName,
						classLevel, difficultSelect, rating, userCode, volatility, playCount,
						newAndOldProblems);

				PacketRegistrationData data = new PacketRegistrationData();
				data.playerId = status.getPlayerId();
				data.playerListIndex = status.getPlayerListId();
				data.sessionId = status.getSessionId();
				return data;
			} catch (Exception e) {
				String parameters = Objects.toStringHelper(this)
						.add("playerSummary", playerSummary).add("genres", genres)
						.add("types", types).add("greeting", greeting).add("gameMode", gameMode)
						.add("roomName", roomName).add("theme", theme)
						.add("imageFileName", imageFileName).add("classLevel", classLevel)
						.add("difficultSelect", difficultSelect).add("rating", rating)
						.add("userCode", userCode).add("volatility", volatility)
						.add("playCount", playCount).add("newAndOldProblems", newAndOldProblems)
						.add("publicEvent", publicEvent).toString();
				logger.log(Level.SEVERE, "プレイヤー登録に失敗しました。" + parameters, e);
				e.printStackTrace();
			}

			return null;
		}
	}

	// マッチング
	@Override
	public PacketMatchingData getMatchingData(int sessionId) {
		return gameManager.getSession(sessionId).getMatchingData();
	}

	// 強制的にゲームをスタートさせる
	@Override
	public int requestSkip(int sessionId, int playerListId) {
		Game session = gameManager.getSession(sessionId);
		session.requestStartingGame(playerListId);
		return session.getNumberOfPlayer();
	}

	// ゲーム開始待機
	// ゲームの開始を待つ
	@Override
	public PacketReadyForGame waitForGame(int sessionId) {
		return gameManager.getSession(sessionId).getReadyForGameStatus();
	}

	// 問題を取得する
	@Override
	public List<PacketProblem> getProblem(int sessionId) {
		return gameManager.getSession(sessionId).getProblem();
	}

	// 他のプレイヤーの名前を取得する
	@Override
	public List<PacketPlayerSummary> getPlayerSummaries(int sessionId) {
		return gameManager.getSession(sessionId).getPlayerSummaries();
	}

	@Override
	public void sendAnswer(int sessionId, int playerListId, String answer, int userCode,
			int responseTime) {
		gameManager.getSession(sessionId).receiveAnswer(playerListId, answer);
		gameLogger.write(Objects
				.toStringHelper(this)
				.add("method", "sendAnswer")
				.add("sessionId", sessionId)
				.add("playerListId", playerListId)
				.add("answer",
						Arrays.deepToString(Objects.firstNonNull(answer, "").split(
								Constant.DELIMITER_GENERAL))).add("userCode", userCode)
				.add("responseTime", responseTime).add("remoteAddress", getRemoteAddress())
				.toString());
	}

	@Override
	public void notifyTimeUp(int sessionId, int playerListId, int userCode) {
		gameLogger.write(Objects.toStringHelper(this).add("method", "notifyTimeUp")
				.add("sessionId", sessionId).add("playerListId", playerListId)
				.add("userCode", userCode).add("remoteAddress", getRemoteAddress()).toString());
	}

	@Override
	public void notifyGameFinished(int userCode, int oldRating, int newRating, int sessionId)
			throws ServiceException {
		gameLogger.write(Objects.toStringHelper(this).add("method", "notifyGameFinished")
				.add("userCode", userCode).add("sessionId", sessionId).add("oldRating", oldRating)
				.add("newRating", newRating).add("remoteAddress", getRemoteAddress()).toString());
	}

	// ゲームの進行状態を取得する
	@Override
	public PacketGameStatus getGameStatus(int sessionId) {
		Game session = gameManager.getSession(sessionId);
		return session.getGameStatus();
	}

	@Override
	public void keepAliveGame(int sessionId, int playerListId) {
		Game session = gameManager.getSession(sessionId);
		session.keepAlive(playerListId);
	}

	// 結果表示
	// 最終結果を取得する
	@Override
	public List<PacketResult> getResult(int sessionId) {
		return gameManager.getSession(sessionId).getPacketResult();
	}

	// チャットにメッセージを送信する
	@Override
	public void sendMessageToChat(PacketChatData chatData) {
		if (badUserManager.isLimitedUser(chatData.userCode, getRemoteAddress())) {
			badUserManager.addLimitedUser(chatData.userCode, getRemoteAddress());
		}

		// ロックはChatManager側で行う
		chatManager.write(chatData, getRemoteAddress());
	}

	@Override
	public PacketChatDataList receiveMessageFromChat(int lastestResId) {
		// ロックはChatManager側で行う
		return chatManager.read(lastestResId);
	}

	// 問題を投稿する
	private final Object fileLock = new Object();

	@Override
	public int uploadProblem(final PacketProblem problem, final int userCode)
			throws ServiceException {
		final String remoteAddress = getRemoteAddress();

		threadPool.execute(new Runnable() {
			@Override
			public void run() {
				synchronized (fileLock) {
					try {
						Files.createParentDirs(PROBLEM_CREATION_LOG_FILE);
					} catch (IOException e) {
						logger.log(Level.WARNING, "問題作成ログディレクトリの作成に失敗しました", e);
					}
					try (PrintStream stream = new PrintStream(new BufferedOutputStream(
							new FileOutputStream(PROBLEM_CREATION_LOG_FILE, true)), false, "MS932")) {
						stream.println(problem.toString());
						stream.println(userCode + "\t" + remoteAddress + "\t"
								+ Calendar.getInstance().getTime());
					} catch (Exception e) {
						logger.log(Level.WARNING, "問題作成ログの書き込みに失敗しました", e);
					}
				}
			}
		});

		// BugTrack-QMAClone/540
		// http://kishibe.dyndns.tv/qmaclone/wiki/wiki.cgi?page=BugTrack-QMAClone%2F540
		// if (badUserManager.isLimitedUser(userCode, remoteAddress)) {
		// badUserManager.addLimitedUser(userCode, remoteAddress);
		// if (problem.id == -1) {
		// return normalModeProblemManager.getNumberOfProblem() + 1;
		// } else {
		// return problem.id;
		// }
		// }

		final int problemID;

		if (problem.id == -1) {
			problemID = wrap("問題の追加に失敗しました", new DatabaseAccessible<Integer>() {
				@Override
				public Integer access() throws DatabaseException {
					return normalModeProblemManager.addProblem(problem);
				}
			});
		} else {
			wrap("問題の更新に失敗しました", new DatabaseAccessible<Void>() {
				@Override
				public Void access() throws DatabaseException {
					normalModeProblemManager.updateProblem(problem);
					return null;
				}
			});
			problemID = problem.id;
		}

		threadPool.execute(new Runnable() {
			@Override
			public void run() {
				try {
					database.addCreationLog(problemID, userCode, remoteAddress);
				} catch (DatabaseException e) {
					logger.log(Level.WARNING, "問題作成ログの保存に失敗しました", e);
				}
			}
		});

		// SNSに投稿
		threadPool.execute(new Runnable() {
			@Override
			public void run() {
				snsClient.postProblem(problem);
			}
		});

		return problemID;
	}

	// 問題を取得する
	@Override
	public List<PacketProblem> getProblemList(final List<Integer> problemIds)
			throws ServiceException {
		return wrap("問題リストの取得に失敗しました", new DatabaseAccessible<List<PacketProblem>>() {
			@Override
			public List<PacketProblem> access() throws DatabaseException {
				return database.getProblem(problemIds);
			}
		});
	}

	@Override
	public int[][] getStatisticsOfProblemCount() {
		return normalModeProblemManager.getTableProblemCount();
	}

	@Override
	public int[][] getStatisticsOfAccuracyRate() {
		return normalModeProblemManager.getTableProblemRatio();
	}

	@Override
	public List<PacketProblem> searchProblem(final String query, final String creator,
			final boolean creatorPerfectMatching, final Set<ProblemGenre> genres,
			final Set<ProblemType> types, final Set<RandomFlag> randomFlags)
			throws ServiceException {
		return wrap("問題の検索に失敗しました", new DatabaseAccessible<List<PacketProblem>>() {
			@Override
			public List<PacketProblem> access() throws DatabaseException {
				return database.searchProblem(query, creator, creatorPerfectMatching, genres,
						types, randomFlags);
			}
		});
	}

	@Override
	public List<PacketProblem> searchSimilarProblem(final PacketProblem problem)
			throws ServiceException {
		return wrap("類似問題の検索に失敗しました", new DatabaseAccessible<List<PacketProblem>>() {
			@Override
			public List<PacketProblem> access() throws DatabaseException {
				return database.searchSimilarProblemFromDatabase(problem);
			}
		});
	}

	@Override
	public int getNewUserCode() throws ServiceException {
		return wrap("新規ユーザーコードの作成に失敗しました", new DatabaseAccessible<Integer>() {
			@Override
			public Integer access() throws DatabaseException {
				int userCode;
				do {
					userCode = random.nextInt(100000000);
				} while (database.isUsedUserCode(userCode));
				return userCode;
			}
		});
	}

	@Override
	public void addProblemIdsToReport(final int userCode, final List<Integer> problemIds)
			throws ServiceException {
		wrap("正解率統計への問題登録に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.addProblemIdsToReport(userCode, problemIds);
				return null;
			}
		});
	}

	@Override
	public void removeProblemIDFromReport(final int userCode, final int problemID)
			throws ServiceException {
		wrap("正解率統計からの問題登録解除に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.removeProblemIdFromReport(userCode, problemID);
				return null;
			}
		});
	}

	@Override
	public void clearProblemIDFromReport(final int userCode) throws ServiceException {
		wrap("正解率統計のクリアに失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.clearProblemIdFromReport(userCode);
				return null;
			}
		});
	}

	@Override
	public List<PacketProblem> getUserProblemReport(final int userCode) throws ServiceException {
		return wrap("正解率統計の読み込みに失敗しました", new DatabaseAccessible<List<PacketProblem>>() {
			@Override
			public List<PacketProblem> access() throws DatabaseException {
				return database.getUserProblemReport(userCode);
			}
		});
	}

	@Override
	public PacketUserData loadUserData(final int userCode) throws ServiceException {
		return wrap("ユーザーデータの読み込みに失敗しました", new DatabaseAccessible<PacketUserData>() {
			@Override
			public PacketUserData access() throws DatabaseException {
				return database.getUserData(userCode);
			}
		});
	}

	@Override
	public void saveUserData(final PacketUserData userData) throws ServiceException {
		wrap("ユーザーデータの保存に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.setUserData(userData);
				return null;
			}
		});
	}

	@Override
	public List<PacketUserData> getLoginUsers() {
		return serverStatusManager.getLoginUsers();
	}

	@Override
	public List<PacketWrongAnswer> getWrongAnswers(final int problemID) throws ServiceException {
		return wrap("プレイヤー解答の読み込みに失敗しました", new DatabaseAccessible<List<PacketWrongAnswer>>() {
			@Override
			public List<PacketWrongAnswer> access() throws DatabaseException {
				return database.getPlayerAnswers(problemID);
			}
		});
	}

	@Override
	public void removePlayerAnswers(final int problemID) throws ServiceException {
		wrap("プレイヤー解答の削除に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.removePlayerAnswers(problemID);
				return null;
			}
		});
	}

	@Override
	public List<List<PacketRankingData>> getRankingData() throws ServiceException {
		return wrap("ランキングデータの読み込みに失敗しました",
				new DatabaseAccessible<List<List<PacketRankingData>>>() {
					@Override
					public List<List<PacketRankingData>> access() throws DatabaseException {
						return database.getRankingData();
					}
				});
	}

	@Override
	public void addIgnoreUserCode(int userCode, int targetUserCode) {
		badUserManager.addIgnoreUserCode(userCode, targetUserCode);
	}

	@Override
	public void removeIgnoreUserCode(int userCode, int targetUserCode) {
		badUserManager.removeIgnoreUserCode(userCode, targetUserCode);
	}

	@Override
	public List<PacketProblemCreationLog> getProblemCreationLog(final int problemId)
			throws ServiceException {
		return wrap("問題作成ログの取得に失敗しました", new DatabaseAccessible<List<PacketProblemCreationLog>>() {
			@Override
			public List<PacketProblemCreationLog> access() throws DatabaseException {
				return database.getProblemCreationHistory(problemId);
			}
		});
	}

	@Override
	public List<PacketBbsResponse> getBbsResponses(final int threadId, final int count)
			throws ServiceException {
		return wrap("BBSレスポンスの取得に失敗しました", new DatabaseAccessible<List<PacketBbsResponse>>() {
			@Override
			public List<PacketBbsResponse> access() throws DatabaseException {
				return database.getBbsResponses(threadId, count);
			}
		});
	}

	@Override
	public List<PacketBbsThread> getBbsThreads(final int bbsId, final int start, final int count)
			throws ServiceException {
		return wrap("BBSスレッドの取得に失敗しました", new DatabaseAccessible<List<PacketBbsThread>>() {
			@Override
			public List<PacketBbsThread> access() throws DatabaseException {
				return database.getBbsThreads(bbsId, start, count);
			}
		});
	}

	@Override
	public void buildBbsThread(final int bbsId, final PacketBbsThread thread,
			final PacketBbsResponse response) throws ServiceException {
		response.remoteAddress = getRemoteAddress();

		if (badUserManager.isLimitedUser(response.userCode, response.remoteAddress)) {
			badUserManager.addLimitedUser(response.userCode, response.remoteAddress);
			return;
		}

		wrap("BBSスレッドの設置に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.buildBbsThread(bbsId, thread, response);
				return null;
			}
		});
	}

	@Override
	public void writeToBbs(final PacketBbsResponse response, final boolean age)
			throws ServiceException {
		response.remoteAddress = getRemoteAddress();

		if (badUserManager.isLimitedUser(response.userCode, response.remoteAddress)) {
			badUserManager.addLimitedUser(response.userCode, response.remoteAddress);
			return;
		}

		wrap("BBSへの書き込みに失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.writeToBbs(response, age);
				return null;
			}
		});
	}

	@Override
	public int getNumberOfBbsThreads(final int bbsId) throws ServiceException {
		return wrap("BBSスレッド数の取得に失敗しました", new DatabaseAccessible<Integer>() {
			@Override
			public Integer access() throws DatabaseException {
				return database.getNumberOfBbsThread(bbsId);
			}
		});
	}

	@Override
	public void addLinkData(final PacketLinkData linkData) throws ServiceException {
		wrap("リンクデータの取得に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.addLinkData(linkData);
				return null;
			}
		});
	}

	@Override
	public List<PacketLinkData> getLinkDatas(final int start, final int count)
			throws ServiceException {
		return wrap("リンクデータの取得に失敗しました", new DatabaseAccessible<List<PacketLinkData>>() {
			@Override
			public List<PacketLinkData> access() throws DatabaseException {
				return database.getLinkDatas(start, count);
			}
		});
	}

	@Override
	public int getNumberOfLinkDatas() throws ServiceException {
		return wrap("リンクデータ数の取得に失敗しました", new DatabaseAccessible<Integer>() {
			@Override
			public Integer access() throws DatabaseException {
				return database.getNumberOfLinkDatas();
			}
		});
	}

	@Override
	public void removeLinkData(final int id) throws ServiceException {
		wrap("リンクデータの削除に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.removeLinkData(id);
				return null;
			}
		});
	}

	@Override
	public void updateLinkData(final PacketLinkData linkData) throws ServiceException {
		wrap("リンクデータの更新に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.updateLinkData(linkData);
				return null;
			}
		});
	}

	@Override
	public int[][] getPrefectureRanking() {
		return prefectureRanking.get();
	}

	@Override
	public void addRatingHistory(final int userCode, final int rating) throws ServiceException {
		wrap("レーティング履歴の追加に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.addRatingHistory(userCode, rating);
				return null;
			}
		});
	}

	@Override
	public List<Integer> getRatingHistory(final int userCode) throws ServiceException {
		return wrap("レーティング履歴の取得に失敗しました", new DatabaseAccessible<List<Integer>>() {
			@Override
			public List<Integer> access() throws DatabaseException {
				return database.getRatingHistory(userCode);
			}
		});
	}

	@Override
	public PacketRatingDistribution getRatingDistribution() {
		return ratingDistribution.get();
	}

	@Override
	public List<List<String>> getThemeModeThemes() {
		return themeModeProblemManager.getThemes();
	}

	@Override
	public Map<String, List<PacketRankingData>> getThemeModeRanking() throws ServiceException {
		return wrap("テーマモードランキングの取得に失敗しました",
				new DatabaseAccessible<Map<String, List<PacketRankingData>>>() {
					@Override
					public Map<String, List<PacketRankingData>> access() throws DatabaseException {
						return database.getThemeModeRankingData();
					}
				});
	}

	@Override
	public List<PacketRoomKey> getEventRooms() {
		return gameManager.getPublicMatchingEventRooms();
	}

	@Override
	public void voteToProblem(final int userCode, final int problemId, final boolean good,
			final String feedback, final String playerName) throws ServiceException {
		wrap("問題への投票に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				voteManager.vote(userCode, problemId, good, feedback, playerName,
						getRemoteAddress(), badUserManager);
				return null;
			}
		});
	};

	@Override
	public void resetVote(int problemId) {
		voteManager.reset(problemId);
	}

	@Override
	public String[] recognizeHandwriting(double[][][] strokes) {
		return recognizer.recognize(strokes);
	}

	@Override
	public Set<String> getAvailableChalactersForHandwriting() {
		return recognizer.getAvailableCharacters();
	}

	@Override
	public void clearProblemFeedback(final int problemId) throws ServiceException {
		wrap("問題フィードバックのクリアに失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.clearProblemFeedback(problemId);
				return null;
			}
		});
	}

	@Override
	public List<String> getProblemFeedback(final int problemId) throws ServiceException {
		return wrap("問題フィードバックの取得に失敗しました", new DatabaseAccessible<List<String>>() {
			@Override
			public List<String> access() throws DatabaseException {
				return database.getProblemFeedback(problemId);
			}
		});
	}

	@Override
	public List<PacketTheme> getThemes() throws ServiceException {
		Map<String, List<String>> themeModeQueries = wrap("テーマの取得に失敗しました",
				new DatabaseAccessible<Map<String, List<String>>>() {
					@Override
					public Map<String, List<String>> access() throws DatabaseException {
						return database.getThemeModeQueries();
					}
				});
		Map<String, IntArray> themeModeProblems = themeModeProblemManager.getThemesAndProblems();

		List<PacketTheme> themes = Lists.newArrayList();
		for (Entry<String, List<String>> entry : themeModeQueries.entrySet()) {
			PacketTheme theme = new PacketTheme();
			theme.name = entry.getKey();
			theme.queries = entry.getValue();
			IntArray problemIds = themeModeProblems.get(theme.name);
			if (problemIds != null) {
				theme.numberOfProblems = problemIds.size();
			}
			themes.add(theme);
		}

		Collections.sort(themes, new Comparator<PacketTheme>() {
			@Override
			public int compare(PacketTheme o1, PacketTheme o2) {
				return o1.name.compareTo(o2.name);
			}
		});

		return themes;
	}

	private Map<String, AtomicInteger> themeModeNotificationCounter = Maps.newHashMap();
	private transient final Runnable commandUpdateThemeModeNotificationCounter = new Runnable() {
		@Override
		public void run() {
			synchronized (themeModeNotificationCounter) {
				Set<Entry<String, AtomicInteger>> entrySet = themeModeNotificationCounter
						.entrySet();
				for (Entry<String, AtomicInteger> entry : entrySet) {
					if (entry.getValue().decrementAndGet() >= 0) {
						continue;
					}

					final String theme = entry.getKey();
					threadPool.execute(new Runnable() {
						@Override
						public void run() {
							snsClient.postThemeModeUpdate(theme);
						}
					});

					entrySet.remove(entry);
				}
			}
		}
	};

	private static final int THEME_MODE_NOTIFICATION_WAIT = 60;

	@Override
	public void addThemeModeQuery(final String theme, final String query) throws ServiceException {
		wrap("テーマモードクエリの追加に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.addThemeModeQuery(theme, query);
				return null;
			}
		});

		synchronized (themeModeNotificationCounter) {
			themeModeNotificationCounter
					.put(theme, new AtomicInteger(THEME_MODE_NOTIFICATION_WAIT));
		}
	}

	@Override
	public void removeThemeModeQuery(final String theme, final String query)
			throws ServiceException {
		wrap("テーマモードクエリの削除に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.removeThemeModeQuery(theme, query);
				return null;
			}
		});

		synchronized (themeModeNotificationCounter) {
			themeModeNotificationCounter
					.put(theme, new AtomicInteger(THEME_MODE_NOTIFICATION_WAIT));
		}
	}

	@Override
	public boolean isThemeModeEditor(final int userCode) throws ServiceException {
		return wrap("テーマモード編集者のチェックに失敗しました", new DatabaseAccessible<Boolean>() {
			@Override
			public Boolean access() throws DatabaseException {
				return themeModeEditorManager.isThemeModeEditor(userCode);
			}
		});
	}

	@Override
	public void applyThemeModeEditor(final int userCode, final String text) throws ServiceException {
		wrap("テーマモード編集者の申請に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				try {
					themeModeEditorManager.applyThemeModeEditor(userCode, text);
				} catch (MessagingException e) {
					logger.log(Level.WARNING, "メールの送信に失敗しました", e);
				}
				return null;
			}
		});
	}

	@Override
	public void acceptThemeModeEditor(final int userCode) throws ServiceException {
		wrap("テーマモード編集者の承認に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				themeModeEditorManager.acceptThemeModeEditor(userCode);
				return null;
			}
		});
	}

	@Override
	public void rejectThemeModeEditor(final int userCode) throws ServiceException {
		wrap("テーマモード編集者の却下に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				themeModeEditorManager.rejectThemeModeEditor(userCode);
				return null;
			}
		});
	}

	@Override
	public List<PacketThemeModeEditor> getThemeModeEditors() throws ServiceException {
		return wrap("テーマモード編集者の取得に失敗しました", new DatabaseAccessible<List<PacketThemeModeEditor>>() {
			@Override
			public List<PacketThemeModeEditor> access() throws DatabaseException {
				return themeModeEditorManager.getThemeModeEditors();
			}
		});
	}

	@Override
	public boolean isApplyingThemeModeEditor(final int userCode) throws ServiceException {
		return wrap("テーマモード編集者の申請状態の取得に失敗しました", new DatabaseAccessible<Boolean>() {
			@Override
			public Boolean access() throws DatabaseException {
				return themeModeEditorManager.isApplyingThemeModeEditor(userCode);
			}
		});
	}

	private String getRemoteAddress() {
		return getThreadLocalRequest().getRemoteAddr();
	}

	@Override
	public Set<Integer> getLimitedUserCodes() throws ServiceException {
		return wrap("制限ユーザーコードの取得に失敗しました", new DatabaseAccessible<Set<Integer>>() {
			@Override
			public Set<Integer> access() throws DatabaseException {
				return database.getLimitedUserCodes();
			}
		});
	}

	@Override
	public void removeLimitedUserCode(final int userCode) throws ServiceException {
		wrap("制限ユーザーコードの削除に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.removeLimitedUserCode(userCode);
				return null;
			}
		});
	}

	@Override
	public void addLimitedUserCode(final int userCode) throws ServiceException {
		wrap("制限ユーザーコードの追加に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.addLimitedUserCode(userCode);
				return null;
			}
		});
	}

	@Override
	public void clearLimitedUserCode() throws ServiceException {
		wrap("制限ユーザーコードのクリアに失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.clearLimitedUserCodes();
				return null;
			}
		});
	}

	@Override
	public Set<String> getLimitedRemoteAddresses() throws ServiceException {
		return wrap("制限IPの取得に失敗しました", new DatabaseAccessible<Set<String>>() {
			@Override
			public Set<String> access() throws DatabaseException {
				return database.getLimitedRemoteAddresses();
			}
		});
	}

	@Override
	public void removeLimitedRemoteAddress(final String remoteAddress) throws ServiceException {
		wrap("制限IPの削除に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.removeLimitedRemoteAddress(remoteAddress);
				return null;
			}
		});
	}

	@Override
	public void addLimitedRemoteAddress(final String remoteAddress) throws ServiceException {
		if (remoteAddress.equals("127.0.0.1")) {
			return;
		}
		wrap("制限IPの追加に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.addLimitedRemoteAddress(remoteAddress);
				return null;
			}
		});
	}

	@Override
	public void clearLimitedRemoteAddress() throws ServiceException {
		wrap("制限IPのクリアに失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				database.clearLimitedRemoteAddresses();
				return null;
			}
		});
	}

	@Override
	public int getNumberOfChatLog() throws ServiceException {
		return wrap("チャットログの長さの取得に失敗しました", new DatabaseAccessible<Integer>() {
			@Override
			public Integer access() throws DatabaseException {
				return database.getNumberOfChatLog();
			}
		});
	}

	@Override
	public int getChatLogId(final int year, final int month, final int day, final int hour,
			final int minute, final int second) throws ServiceException {
		return wrap("チャットログ日時の検索に失敗しました", new DatabaseAccessible<Integer>() {
			@Override
			public Integer access() throws DatabaseException {
				return database.getChatLogId(year, month, day, hour, minute, second);
			}
		});
	}

	@Override
	public List<PacketChatData> getChatLog(final int start) throws ServiceException {
		return wrap("チャットログの取得に失敗しました", new DatabaseAccessible<List<PacketChatData>>() {
			@Override
			public List<PacketChatData> access() throws DatabaseException {
				return database.getChatLog(start);
			}
		});
	}

	@Override
	public List<PacketImageLink> getWrongImageLinks() throws ServiceException {
		return wrap("リンク切れ画像の取得に失敗しました", new DatabaseAccessible<List<PacketImageLink>>() {
			@Override
			public List<PacketImageLink> access() throws DatabaseException {
				return imageManager.getErrorImageLinks();
			}
		});
	}

	@Override
	public boolean canUploadProblem(final int userCode) throws ServiceException {
		return wrap("問題投稿制限のチェックに失敗しました", new DatabaseAccessible<Boolean>() {
			@Override
			public Boolean access() throws DatabaseException {
				long dateFrom = System.currentTimeMillis() - 60 * 60 * 1000;
				return database.getNumberOfCreationLogWithMachineIp(getRemoteAddress(), dateFrom) <= Constant.MAX_NUMBER_OF_CREATION_PER_HOUR
						&& database.getNumberOfCreationLogWithUserCode(userCode, dateFrom) <= Constant.MAX_NUMBER_OF_CREATION_PER_HOUR;
			}
		});
	}

	@Override
	public List<PacketProblem> getIndicatedProblems() throws ServiceException {
		return wrap("指摘された問題の取得に失敗しました", new DatabaseAccessible<List<PacketProblem>>() {
			@Override
			public List<PacketProblem> access() throws DatabaseException {
				return database.getIndicatedProblems();
			}
		});
	}

	@Override
	public void indicateProblem(final int problemId) throws ServiceException {
		wrap("指摘フラグの更新に失敗しました", new DatabaseAccessible<Void>() {
			@Override
			public Void access() throws DatabaseException {
				List<PacketProblem> problems = database.getProblem(ImmutableList.of(problemId));
				PacketProblem problem = problems.get(0);
				problem.indication = new Date();
				database.updateProblem(problem);
				return null;
			}
		});
	}

	private interface DatabaseAccessible<T> {
		T access() throws DatabaseException;
	}

	private <T> T wrap(String message, DatabaseAccessible<T> accessor) throws ServiceException {
		try {
			return accessor.access();
		} catch (DatabaseException e) {
			logger.log(Level.WARNING, message, e);
			throw new ServiceException(Throwables.getStackTraceAsString(e));
		}
	}
}
